package com.xz.controller.front;

import com.xz.R;
import com.xz.entity.Member;
import com.xz.entity.frontvo.MemberInfo;
import com.xz.entity.frontvo.RegisterVo;
import com.xz.service.MemberService;
import com.xz.utils.JwtUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/video/frontmember")
public class FrontMemberController {
    @Autowired
    private MemberService memberService;

    //登录
    @PostMapping("login")
    public R loginUser(@RequestBody Member member) {
        //member对象封装手机号和密码
        //调用service方法实现登录
        //返回token值，使用jwt生成
        String token = memberService.login(member);
        return R.ok().data("token",token);
    }

    //注册
    @PostMapping("register")
    public R registerUser(@RequestBody RegisterVo registerVo) {
        memberService.register(registerVo);
        return R.ok();
    }

    //根据token获取用户信息
    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request) {
        //调用jwt工具类的方法。根据request对象获取头信息，返回用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //查询数据库根据用户id获取用户信息
        Member member = memberService.getById(memberId);
        return R.ok().data("userInfo",member);
    }

    //根据用户id获取用户信息
    @GetMapping("getUserInfo/{id}")
    public R getUserInfo(@PathVariable String id){
        Member member = memberService.getById(id);
        return R.ok().data("member",member);
    }

    //修改用户的基本信息
    @PostMapping("updateUserInfo/{id}")
    public boolean updateUserInfo(@RequestBody MemberInfo memberInfo,@PathVariable String id){
        Member member = memberService.getById(id);
        BeanUtils.copyProperties(memberInfo,member);
        return memberService.updateById(member);
    }
}
